// pages/my_stylists/my_stylists.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    shopimgUrl: app.globalData.shopimgUrl,
    nickname: "",
    page: 1,
    hairStylist: [],
    height: 0,
    status: 0,
    nohandlerNum: 0,
    sortType: "0",
    cityId: "",
    userId: "",
    searchStoreName: "",
    mainActiveIndex: 0,
    activeId: 1,
    showkm: '',
    showArealistright: [],
    showArealist: [{
      name: '附近',
      id: 0,
      list: [
        '0.5',
        '1',
        '2',
        '3',
        '4',
        '5',
        '10',
        '20',
      ]
    }, ],
    multiList: [{
        text: '综合排序',
        value: 0
      },
      {
        text: '距离最近',
        value: 1
      },
      {
        text: '月接单最多',
        value: 2
      },
      {
        text: '评论最多',
        value: 3
      },
      {
        text: '价格最低',
        value: 4
      },
      {
        text: '价格最高',
        value: 5
      }
    ],
    multiListindex: 3,
    multiValue: 3,
    page: 1,
    height: 0,
    active: 0,
    shownearby: false,
    name: '附近',
    name1: '评论最多',
    districtId: '',
    cityId: '',
    maxDistance: '',
    categoryIds: null,
    gradeId: null,
    showAreaindex: 0,
    nearbyVisible: false,
    areaVisible: false,
    position: '',
    coupon: '',
    showModal: false,
    couponList: [{
        title: '美发师等级',
        list: [{
            name: '高级',
            bool: false,
            status: 1,
          },
          {
            name: '资深',
            bool: false,
            status: 1,
          },
          {
            name: '首席',
            bool: false,
            status: 1,
          },
          {
            name: '总监',
            bool: false,
            status: 1,
          },
          {
            name: '督导',
            bool: false,
            status: 1,
          },
          {
            name: '助理',
            bool: false,
            status: 1,
          }
        ]
      },
      {
        title: '特惠活动',
        list: [{
            name: '优惠券',
            bool: false,
            status: 2,
            type: 1
          },
          {
            name: '套餐券',
            bool: false,
            status: 2,
            type: 2
          },
        ]
      }
    ],
    serviceItem: [],
  },

  /**
   * 弹窗
   */
  showDialogBtn: function () {
    this.setData({
      showModal: true,
      shownearby: false,
      showsort: false,
      couponList: this.data.couponList.map(item => {
        item.list.map(item => {
          item.bool = false
          return item
        })
        return item
      }),
      position: '',
      coupon: ''
    })
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false,
    });
  },

  onCancel: function () {
    this.setData({
      categoryIds: '',
      gradeId: '',
      page: 1
    })
    this.storeStylistsByNexus()
  },

  // 获取选择中服务项目
  getserviceItemArr() {
    let list = ''
    let list1 = ''
    this.data.couponList.map(item => {
      if (item.title == '美发师等级') {
        item.list.map(item => {
          if (item.bool == true) {
            list = item
          }
        })
      } else {
        item.list.map(item => {
          if (item.bool == true) {
            list1 = item
          }
        })
      }

    })
    this.setData({
      categoryIds: list,
      gradeId: '',
    })
  },

  // 点击高亮项目
  onShowchecked(e) {
    const {
      items
    } = e.currentTarget.dataset
    this.setData({
      couponList: this.data.couponList.map(item => {
        if (item.title == '美发师等级' && items.status == 1) {
          item.list.map(item => {
            if (item.name == items.name) {

              item.bool = !items.bool
              console.log(item);
            } else {
              item.bool = false
            }
            return item
          })
          return item
        } else if (item.title == '特惠活动' && items.status == 2) {
          item.list.map(item => {
            if (item.name == items.name) {
              console.log(item);
              item.bool = !items.bool
            } else {
              item.bool = false
            }
            return item
          })
          return item
        } else {
          return item
        }
      })

    })
    // 获取选择中服务项目
    // this.getserviceItemArr()
  },

  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
    let data1 = ''
    let data2 = ''
    this.data.couponList.map(item => {
      if (item.title == '美发师等级') {
        item.list.map(item => {
          if (item.bool) {
            data1 = item.name
          }
        })
      } else if (item.title == '特惠活动') {
        item.list.map(item => {
          if (item.bool) {
            data2 = item.type
          }
        })
      }
    })
    this.setData({
      position: data1,
      coupon: data2,
      page: 1
    })
    this.storeStylistsByNexus()
  },

  // 获取实时输入框的值
  onChangeinput(e) {
    this.setData({
      areaVisible:false,
      nearbyVisible:false,
      shownearby:false,
    })
    if (e.detail.length == 0) {
      this.setData({
        multiListindex: 3,
        multiValue: 3,
        page: 1,
        districtId: '',
        maxDistance: '',
        categoryIds: '',
        searchStoreName: '',
      })
      // 查询数据
      this.storeStylistsByNexus()
    }
  },

  // 点击x按钮
  nicknameblur(e) {
    this.setData({
      multiListindex: 3,
      multiValue: 3,
      page: 1,
      districtId: '',
      maxDistance: '',
      categoryIds: '',
      searchStoreName: ''
    })
    // 查询数据
    this.storeStylistsByNexus()
  },
  // 展示附近弹窗
  showNearby(e) {
    this.setData({
      shownearby: true,
      showArealistright: this.data.showArealist[0].list,
      areaVisible: true,
      nearbyVisible: false
    })
  },

  // 综合弹窗
  showsort(e) {
    this.setData({
      shownearby: true,
      nearbyVisible: true,
      areaVisible: false
    })
  },
  // 附近和综合弹窗
  onSelectArea(e) {
    const {
      list,
      index,
      id,
      name,
      level
    } = e.currentTarget.dataset
    console.log(e);
    if (level == 2) {
      this.setData({
        showArealistright: list,
        showAreaindex: index,
        name,
        districtId: '',
        maxDistance: '',
        page: 1
      })
    } else if (level == 3) {
      this.setData({
        showArealistright: list,
        showAreaindex: index,
        name,
        districtId: id,
        maxDistance: '',
        page: 1
      })
    } else {
      this.setData({
        showArealistright: list,
        showAreaindex: index,
        name,
        districtId: '',
        maxDistance: '',
        page: 1
      })
    }
    // 点击地点
    if (index !== 0) {
      this.storeStylistsByNexus()
    }
  },

  // 点击附近距离
  onDistance(e) {
    const {
      id
    } = e.currentTarget.dataset
    console.log(e);
    this.setData({
      maxDistance: id * 1000,
      districtId: '',
      page: 1,
      showkm: id
    })
    console.log(this.data);
    this.storeStylistsByNexus()
  },

  // 点击排行事件
  onSelectRank(e) {
    const {
      list,
      index
    } = e.currentTarget.dataset
    console.log(e);
    // 排序类型，0综合排序，1距离最近，2月结单量最多，3评论量最多
    this.setData({
      multiValue: list.value,
      multiListindex: index,
      name1: list.text,
      page: 1
    })
    this.storeStylistsByNexus()
  },

  // tab切换事件
  onChange(e) {
    console.log(e);
    this.setData({
      active: e.detail.index
    })
  },

  // 切换邀请入驻
  onhideoverlay() {
    this.setData({
      active: 1,
      shownearby: false
    })
  },

  // 关闭附近和综合弹窗
  onClickHidenearbys() {
    this.setData({
      shownearby: false,
      nearbyVisible: false,
      areaVisible: false
    })
  },

  // 获取区域
  getAreaByList() {
    _request.postObj('/area/getAreaByStoreId', {
      storeId: wx.getStorageSync('storeId')
    }).then((res) => {
      this.setData({
        showArealist: [...this.data.showArealist, ...res.data.data],
        cityId: res.data.data[0].id,
        page: 1
      })
      // 查询数据

    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 搜索门店
  onSearch(e) {
    console.log('1233333333333333333333333333')
    this.setData({
      searchStoreName: e.detail,
      page: 1
    })
    this.storeStylistsByNexus();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    });
    // 获取区域
    this.getAreaByList()
    //this.data.hairStylist = [];
    this.storeStylistsByNexus();
    // this.getNohandlerNum();
  },
  nicknameblur: function (e) {
    this.setData({
      nickname: e.detail.value
    })
  },
  // 清空
  onClear: function () {
    this.setData({
      hairStylist: [],
      page: 1,
      nickname: ''
    })
    this.storeStylistsByNexus();
  },
  // 搜索
  queryStylist: function () {
    this.setData({
      hairStylist: [],
      page: 1
    })
    this.storeStylistsByNexus();
  },
  // 详情
  onQueryDetail(e) {
    const {
      item
    } = e.currentTarget.dataset
    console.log(item);
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/hairstylistdetails/index?hair=' + encodeURIComponent(obj) + '&status=' + this.data.status,
    })
  },
  // 店内，平台切换
  queryListByType(event) {
    console.log(event);
    this.setData({
      hairStylist: [],
      page: 1,
      status: event.detail.index,
      nickname: ''
    })
    this.storeStylistsByNexus()
  },
  // 查询未处理的申请数
  getNohandlerNum() {
    _request.postObj('/storeStylist/inviteStylist', {
      storeId: wx.getStorageSync('storeId'),
      sortType: "1",
      nickname: '',
      page: 1,
      status: 1,
    }).then((res) => {
      this.setData({
        nohandlerNum: res.data.data.total
      })
    });
  },
  // 查询美发师列表
  storeStylistsByNexus() {
    const {
      userId,
      page,
      categoryIds,
      cityId,
      searchStoreName,
      maxDistance,
      multiValue,
      districtId,
      stores,
      position,
      coupon,
      status
    } = this.data
    _request.postObj('/storeStylist/inviteStylist', {
      storeId: wx.getStorageSync('storeId'),
      nickname: searchStoreName,
      status: status,
      sortType: multiValue, // 排序类型，0综合排序，1距离最近，2月结单量最多，3评论量最多
      cityId: cityId, // 城市id
      districtId: districtId, //  区ID，默认0
      position: position,
      coupon: coupon,
      page: page,
      size: 10,
    }).then((res) => {
      if (res.data.data != null) {
        var stylists = res.data.data;
        if (this.data.status == 1) {
          stylists = res.data.data.rows;
        }
        let list = stylists.map(item => {
          let distance = (parseInt(item.distance) / 1000).toFixed(2) * 1000;
          let newdistance = (Math.round(distance * 10) / 10000).toFixed(1)
          if (item.lable) {
            let money = item.lable.match(/\d+(.\d+)?/g).join('');
            let meal = item.lable.split(money)[0]
            return {
              ...item,
              money,
              meal,
              'newdistance': newdistance
            }
          } else {
            return {
              ...item,
              'newdistance': newdistance
            }
          }
        });
        var listTemps = this.data.hairStylist;
        for (let i = 0; i < list.length; i++) {
          listTemps.push(list[i]);
        }
        this.setData({
          hairStylist: page > 1 ? listTemps: list,
          shownearby: false
        })
        if (this.data.status == 1) {
          this.setData({
            nohandlerNum: res.data.data.total
          })
        }
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  more: function () {
    console.log('加载更多');
    this.data.page++;
    this.storeStylistsByNexus();
  }
})